package com.hk.test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.hk.config.MyConfiguration;
import com.hk.sqlsession.MySqlSessionFactory;
import com.hk.sqlsession.MySqlSessionFactoryBuilder;


public class test02{

	/**
	 * 用于测试连接以及查询是否正常
	 * @param args
	 */
 public static void main(String[] args) {
	    String mybatisXml = "datasource-mybatis.xml";
		InputStream in1 = test02.class.getClassLoader().getResourceAsStream(mybatisXml);
		String path = "UserMapper.xml";
		InputStream in2 = test02.class.getClassLoader().getResourceAsStream(path);
		MySqlSessionFactory sqlSessionFactory = new MySqlSessionFactoryBuilder().build(in1,in2);
		MyConfiguration config = sqlSessionFactory.getConfig();
		Connection conn = config.getConn();
		try {
			String sql = "select id,username from t_user where id = ?";
//			Statement stmt = conn.createStatement();
//			ResultSet executeQuery = stmt.executeQuery(sql);
			PreparedStatement pre =null;
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, 3);
			ResultSet executeQuery = ps.executeQuery();
			while(executeQuery.next())
				System.out.println(executeQuery.getObject(1));
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
